/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ssau.berlin.ejb.facade;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import ru.ssau.berlin.entity.GroupAttribute;
import ru.ssau.berlin.entity.GroupParameter;
import ru.ssau.berlin.entity.GroupType;
import ru.ssau.berlin.entity.PersonGroup;

/**
 *
 * @author Caesar
 */
@Stateless
public class GroupFacade implements GroupFacadeLocal {
    
    @PersistenceContext
    private EntityManager em;

    public void create(PersonGroup personGroup) {
        em.persist(personGroup);
    }

    public void edit(PersonGroup personGroup) {
        em.merge(personGroup);
    }

    public void remove(PersonGroup personGroup) {
        PersonGroup temp = em.merge(personGroup);
        for (GroupParameter parameter: temp.getParameters()) {
            em.remove(parameter);
        }
        em.remove(temp);
    }

    public PersonGroup find(long id) {
        return em.find(PersonGroup.class, id);
    }

    public List<PersonGroup> findAll() {
        return em.createNamedQuery("findAllGroups").getResultList();
    }

    public List<PersonGroup> findByName(String name) {
        return em.createNamedQuery("findGroupByName")
                .setParameter("groupName", name).getResultList();
    }

    public List<PersonGroup> findFaculties() {
        return em.createNamedQuery("findFaculties")
                .getResultList();
    }

    public List<PersonGroup> findDepartments() {
        return em.createNamedQuery("findDepartments")
                .getResultList();
    }

    public List<PersonGroup> findStudentGroups() {
        return em.createNamedQuery("findStudentGroups").getResultList();
    }

    public GroupType findGroupType(String name) {
        return (GroupType) em.createNamedQuery("findGroupTypeByName")
                .setParameter("typeName", name).getSingleResult();
    }

    public GroupType findGroupType(long id) {
        return em.find(GroupType.class, id);
    }

    public List<GroupType> findGroupTypes() {
        return em.createNamedQuery("findGroupTypes").getResultList();
    }

    public void createType(GroupType type) {
        em.persist(type);
    }

    public void editType(GroupType type) {
        em.merge(type);
    }

    public void removeType(GroupType type) {
        em.remove(em.merge(type));
    }

    public GroupAttribute findGroupAttribute(long id) {
        return em.find(GroupAttribute.class, id);
    }

    public List<GroupAttribute> findGroupAttributes() {
        // List<GroupAttribute> result = em.createNamedQuery("findGroupAttributes").getResultList();
        // for lazy list evaluation in the current session
//        for (GroupAttribute attr : result) {
//            attr.getGroupTypes().size();
//        }
        return em.createNamedQuery("findGroupAttributes").getResultList();
    }

    public void createAttribute(GroupAttribute attr) {
        em.persist(attr);
    }

    public void editAttribute(GroupAttribute attr) {
        em.merge(attr);
    }

    public void removeAttribute(GroupAttribute attr) {
        em.remove(em.merge(attr));
    }

    public void createParameter(GroupParameter parameter) {
        em.persist(parameter);
    }

    public GroupParameter findParameter(long id) {
        return em.find(GroupParameter.class, id);
    }

    public void removeParameter(GroupParameter parameter) {
        parameter = findParameter(parameter.getId());
        em.remove(parameter);
    }

}
